package cn.com.zsw.gblog.modules.user.entity;

import cn.com.zsw.gblog.modules.common.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @Author: shiwangZhou
 * @Description:
 * @Date: 2020-07-01 14:21
 */
@EqualsAndHashCode(callSuper = true)
@TableName("gb_sys_user")
@Data
public class GbUserEntity extends BaseEntity implements UserDetails {

    private String username;
    private String nickname;
    private String password;
    /**
     * 头像
     */
    private String avatar;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;

    /**
     * 角色信息
     */
    @TableField(exist = false)
    private Set<? extends GrantedAuthority> authorities;

    @TableField(exist = false)
    private List<RoleEntity> roles;


    public GbUserEntity(String username, String password, Set<? extends GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.authorities = authorities;
    }



    public GbUserEntity() {

    }

    public GbUserEntity(String username, Set<? extends GrantedAuthority> authorities, List<RoleEntity> roles) {
        this.username = username;
        this.authorities = authorities;
        this.roles = roles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        for (Role role : getRoles()) {
//            authorities.add(new SimpleGrantedAuthority(role.getName()));
//        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }


    @Override
    public String getUsername() {
        return username;
    }


    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }


    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }


    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }


    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setAuthorities(Set<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setRoles(List<RoleEntity> roles) {
        this.roles = roles;
    }
    public List<RoleEntity> getRoles() {
        return roles;
    }



}
